package fr.inserm.u915.organifooyi.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity(name="reception")
@Table(name="receptionTubes")
@NamedQueries({
    @NamedQuery(name="reception.list.all",
            query="select R from reception as R ORDER BY R.cdNumber"),
    @NamedQuery(name="reception.find.by.cdNumber",
            query="select R from reception as R where R.cdNumber= :cdNumber ")

	})
public class ReceptionTubes
	extends AbstractUpdatableObject
	{
	public static final String REGEX1="CD\\d\\d\\d\\d\\d";
	public static final String REGEX2="[A-Z]\\d\\d\\d\\d";
	public static final String REGEX3="\\d\\dH\\d\\d\\d\\d";

	
	@Column(name="countInit")
	private Short countInit;
	@Column(name="commentaire")
	private String commentaire;
	@Column(name="dateReception")
	private String dateReception;
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="individual_id")
	private Individual individual;
	@OneToMany(mappedBy="receptionTubes",fetch=FetchType.LAZY,cascade=CascadeType.PERSIST)
	private List<Tube> tubes=new ArrayList<Tube>();
	@Column(name="cdNumber")
	private String cdNumber;
	
	@OneToMany(mappedBy="reception",fetch=FetchType.LAZY,cascade=CascadeType.PERSIST)
	private List<Extraction> extractions=new ArrayList<Extraction>();

	
	public Short getCountInit()
		{
		return countInit;
		}
	public void setCountInit(Short countInit)
		{
		this.countInit = countInit;
		}
	public String getCommentaire() {
		return commentaire;
	}
	public void setCommentaire(String commentaire) {
		this.commentaire = commentaire;
	}
	public String getDateReception() {
		return dateReception;
	}
	public void setDateReception(String dateReception) {
		this.dateReception = dateReception;
		}
	public Individual getIndividual()
		{
		return individual;
		}
	public void setIndividual(Individual individual)
		{
		this.individual = individual;
		}
	
	public List<Tube> getTubes() {
		return tubes;
		}
	
	public void setTubes(List<Tube> tubes) {
		this.tubes = tubes;
		}
	public String getCdNumber()
		{
		return cdNumber;
		}
	public void setCdNumber(String cdNumber)
		{
		this.cdNumber = cdNumber;
		}
	
	public List<Extraction> getExtractions()
		{
		return extractions;
		}
	
	public void setExtractions(List<Extraction> extractions)
		{
		this.extractions = extractions;
		}
	
	@Transient
	public String isValidCDNumber()
		{
		if(this.getCdNumber()==null) return "CD number is null";
		String s=this.getCdNumber().trim();
		if(s.isEmpty()) return "CD number is empty";
		for(String regex: new String[]{REGEX1,REGEX2,REGEX3})
			{
			if(s.matches(regex)) return null;
			}
		return "\""+s+"\" doesn't match "+REGEX1+" or "+REGEX2+" or "+REGEX3;
		}

	
	@Override
	public String toString()
		{
		return getCdNumber();
		}
	}

